﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Editor
{

    class EditorApplication
    {
        private IDK.Engine mEngine = new IDK.Engine();

        void Start()
        {
            Game.Log.Init("Logs", "0");
            Game.Log.Warn("Log override mode");

            mEngine = new IDK.Engine();
            mEngine.Init();
            Game.Log.Hint("Engine create");
        }

        void Stop()
        {
            //detach viewport
            mEngine.GetRenderer().SetViewport(null);
            mEngine.Stop();

            mEngine = null;
        }

        public void Run()
        {
            Start();
            RunWindow();

            //waiting gc
            GC.Collect();
            GC.WaitForPendingFinalizers();

            //stop
            Stop();
        }

        void RunWindow()
        {
            var mainForm = new MainForm();

            mainForm.Engine = mEngine;

            IDK.Timer mTimer = new IDK.Timer();

            mainForm.Show();

            Game.Events.ProcessUpdate evntUpdate = new Game.Events.ProcessUpdate();

            float timeElapsed = 0;
            while (mainForm.Created)
            {
                float MinTimeStep = 1.0f / 10.0f;
                float MaxTimeStep = 1.0f / 50.0f;

                float dt = mTimer.GetElapsed();

                if (dt > MinTimeStep)
                {
                    dt = MinTimeStep;
                }

                timeElapsed += dt;

                if (timeElapsed > MaxTimeStep)
                {
                    evntUpdate.ElapsedTime = timeElapsed;
                    mainForm.UpdateEngine(evntUpdate);
                    timeElapsed = 0;
                }
                else
                {
                    float timeSleep = (MaxTimeStep - timeElapsed) * 1000.0f;
                    //Log.Hint(timeSleep.ToString());
                    mTimer.Sleep((uint)(timeSleep));
                }

                //Simple way need using true way : http://blogs.msdn.com/b/rickhos/archive/2005/03/30/403952.aspx
                System.Windows.Forms.Application.DoEvents();
            }
        }
    }

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        /// 
        [STAThread]
        static void Main()
        {
            IDK.MemoryManager.Init();

            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Run();

            GC.Collect();
            GC.WaitForPendingFinalizers();

            IDK.MemoryManager.ShowLeaks();
        }

        static void Run()
        {
            EditorApplication editor = new EditorApplication();
            editor.Run();
        }
    }
}
